package com.tiantian.quanminxiu.dialog;

import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tiantian.quanminxiu.R;
import com.tiantian.quanminxiu.view.EditInfoView;

@SuppressLint("NewApi")
public class Dialog_EditSex {

	/**
	 * 重置密码弹出框
	 *
	 * @author Administrator
	 *
	 */
	private static PopupWindow popupWindow;// 根弹出框
	private static View contentView;
	static Context context;
	private static RelativeLayout pop_chosepic_root;
	private static TextView tv_popchosepic_cancle;// 取消按钮
	private static TextView tv_popchosepic_getpic;// 从相册获取照片
	private static TextView tv_popchosepic_takepic;// 照相
	public static String sex = "";

	private static EditInfoView mEditInfoView;
	/**
	 * 构造
	 *
	 * @param context
	 */
	private Dialog_EditSex(Context context) {
		initPop(context);
	}

	/**
	 * 初始化组件
	 *
	 * @param context
	 */
	private static void initPop(Context context) {
		contentView = LayoutInflater.from(context).inflate(
				R.layout.dialog_editsex, null);
		pop_chosepic_root = (RelativeLayout) contentView
				.findViewById(R.id.pop_chosepic_root);
		tv_popchosepic_cancle = (TextView) contentView
				.findViewById(R.id.tv_popchosepic_cancle);
		tv_popchosepic_getpic = (TextView) contentView
				.findViewById(R.id.tv_popchosepic_getpic);
		tv_popchosepic_takepic = (TextView) contentView
				.findViewById(R.id.tv_popchosepic_takepic);
		popupWindow = new PopupWindow();
		// 设置SelectPicPopupWindow弹出窗体的宽
		popupWindow.setWidth(LayoutParams.FILL_PARENT);
		// 设置SelectPicPopupWindow弹出窗体的高
		popupWindow.setHeight(LayoutParams.FILL_PARENT);
		// 设置SelectPicPopupWindow弹出窗体可点击
		popupWindow.setFocusable(true);
		// 设置SelectPicPopupWindow弹出窗体动画效果
		popupWindow.setContentView(contentView);
//		popupWindow.setAnimationStyle(R.style.AnimBottom);
		popupWindow.setOutsideTouchable(true);
		pop_chosepic_root.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				int height = contentView.findViewById(R.id.pop_chosepic)
						.getTop();
				int y = (int) event.getY();
				if (y < height) {
					hide();
					distroy();
				}
				return true;
			}
		});
		initAction();
	}

	public static void initAction() {
		tv_popchosepic_cancle.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				hide();
				distroy();
			}
		});
		// 从相册获取照片
		tv_popchosepic_getpic.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mEditInfoView.setUserSex(2);
				hide();
				distroy();
			}
		});
		// 拍照
		tv_popchosepic_takepic.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mEditInfoView.setUserSex(1);
				hide();
				distroy();
			}
		});
	}

	/**
	 * 显示pop
	 *
	 * @param view
	 * @param _context
	 */
	public static void show(View view, Context _context, EditInfoView editInfoView) {
		context = _context;
		mEditInfoView = editInfoView;
		if (null != popupWindow) {
			popupWindow.showAtLocation(view, Gravity.BOTTOM
					| Gravity.CENTER_HORIZONTAL, 0, 0);
		} else {
			initPop(context);
			popupWindow.showAtLocation(view, Gravity.BOTTOM
					| Gravity.CENTER_HORIZONTAL, 0, 0);
		}

	}

	/**
	 * 隐藏弹出框
	 */
	public static void hide() {
		if (null != popupWindow) {
			popupWindow.dismiss();
		}
	}

	/**
	 * 销毁弹出框
	 */
	public static void distroy() {
		popupWindow = null;
	}

}
